<template>
  <div class="flex-row flex h-full">
    <!-- <div class="fixed top-0 left-0 xl:hidden py-2 px-4 text-lg">
      <img src="../assets/logo.svg" class="h-10" />
    </div> -->
    <div class="hidden xl:block h-full relative flex-1 overflow-hidden 2xl:flex-none">
      <img src="../assets/login-bg.jpg" class="login-bg h-full absolute top-0 left-0 2xl:static" />
      <div
        class="absolute -translate-x-1/2 left-1/2 top-0 bottom-0 flex justify-center items-center 2xl:left-32 2xl:translate-x-0"
      >
        <!-- <div class="absolute top-0 left-0 pt-6">
          <img src="../assets/sl-text.svg" class="h-10" />
        </div> -->
        <div class="flex-1 whitespace-nowrap">
          <h1 class="sys-title font-semibold"> 高速公路收费员服务质量管理软件 </h1>
          <h2 class="sys-des font-semibold">
            {{ t("aiot.login.sysDes") }}
          </h2>
        </div>
      </div>
    </div>
    <!--  xl:min-w-login-right -->
    <div class="flex-1 flex h-full items-center justify-center relative overflow-hidden">
      <img src="../assets/login-bg.jpg" class="absolute top-0 h-full xl:hidden left-1/2 -translate-x-1/2" />
      <div class="flex-1 relative z-10">
        <h1 class="login-sys-title font-semibold xl:hidden"> 高速公路收费员<br />服务质量管理软件 </h1>
        <div class="login bg-white px-4 py-12 xxs:px-9 xl:p-16">
          <h2 class="text-color-main text-center"> {{ t("aiot.login.userLogin") }} </h2>
          <a-form ref="formRef" :hide-required-mark="true" name="login" :model="formState" @finish="login">
            <a-form-item name="account" :rules="getRules(t('aiot.login.account'))">
              <a-input v-model:value="formState.account" autocomplete="on" :placeholder="t('aiot.login.account')" />
            </a-form-item>
            <a-form-item name="password" :rules="getRules(t('aiot.login.pwd'))" class="relative z-10">
              <a-input v-model:value="formState.password" type="password" :placeholder="t('aiot.login.pwd')" />
            </a-form-item>
            <a-form-item>
              <a-button type="primary" html-type="submit" block :loading="loading" :disabled="loginBtnDiabled">
                {{ loginBtnDiabled ? "登录成功，跳转中..." : t("aiot.login.login") }}
              </a-button>
              <a-typography-text v-show="errMsg" type="danger">
                {{ errMsg }}
              </a-typography-text>
            </a-form-item>
            <a-form-item name="rememberMe" class="!mb-0">
              <a-checkbox v-model:checked="formState.rememberMe">{{ t("aiot.login.remMe") }}</a-checkbox>
            </a-form-item>
          </a-form>
        </div>
      </div>
      <a class="block absolute right-0 top-0 p-2" href="javascript:void(0)" @click="setIp"> 设置服务器IP </a>
    </div>
    <a-modal v-model:visible="ipVisible" title="设置服务器IP" @ok="ipOk">
      <a-input v-model:value="ipVal" placeholder="请输入ip地址" />
    </a-modal>
  </div>
</template>
<script lang="ts" setup>
import { nextTick, onMounted, reactive, ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import authCache from "/@/utils/cache/authCache";
import router, { FIRST_ROUTE } from "/@/router";
import { getUserInfo, handleLogin } from "/@/api/home/user";
import { message } from "ant-design-vue";

// const router = useRouter();
const { t } = useI18n();
const formRef = ref();
const loading = ref(false);
const errMsg = ref("");
const loginBtnDiabled = ref(false);
//captcha start
// const captchCheckStatus = ref(0);
// const captchaUrl = ref(DEFAULT_IMG_BASE64_STR);
// const isCaptchaLoading = ref(false);
// const onCaptchaLoad = () => {
//   isCaptchaLoading.value = false;
// };
// const clickCaptcha = async () => {
//   if (isCaptchaLoading.value) return;
//   isCaptchaLoading.value = true;
//   const url = await store.dispatch("user/getCaptchaImgUrl");
//   captchaUrl.value = url;
// };
// const onCaptchaError = () => {
//   captchaUrl.value = DEFAULT_IMG_BASE64_STR;
//   message.error(t("aiot.login.captchaLoadErr"));
// };
onMounted(() => {
  // clickCaptcha();
});
//captcha end
const formState = reactive({
  account: "",
  password: "",
  code: "",
  rememberMe: false,
});
watch(
  () => formState.account,
  () => {
    errMsg.value = "";
  }
);
watch(
  () => formState.password,
  () => {
    errMsg.value = "";
    isRemPass = false;
  }
);
watch(
  () => formState.code,
  () => {
    errMsg.value = "";
  }
);
const login = async () => {
  if (!authCache.getServerIp()) {
    ipVisible.value = true;
    return message.info("请先设置服务器IP地址");
  }
  loading.value = true;
  errMsg.value = "";
  try {
    const re = await handleLogin({
      username: formState.account,
      password: formState.password,
    });
    if (re) {
      loginBtnDiabled.value = true;
      authCache.setToken(re.token as string);
      const info = await getUserInfo();
      if (info) {
        const user = info.user;
        user.account = formState.account;
        authCache.setUserInfo(user);
        router.push(FIRST_ROUTE);
      }
      if (formState.rememberMe) {
        authCache.setRemUserInfo({
          account: formState.account,
          password: formState.password,
        });
      } else {
        authCache.setRemUserInfo(null);
      }
    }
  } catch (err: any) {
    if (err) {
      errMsg.value = err.msg;
    }
  } finally {
    loading.value = false;
  }
};
//是否是从localStorage中获取到的md5密码缓存
let isRemPass = false;
onMounted(() => {
  const userInfo = authCache.getRemUserInfo();
  if (userInfo) {
    formState.rememberMe = true;
    Object.assign(formState, userInfo);
    //注意要nextTick 否则会触发watch 将isRemPass置为false
    nextTick(() => {
      isRemPass = true;
    });
  }
});

function getRules(text) {
  return [{ required: true, message: "请输入" + text }];
}
const ipVisible = ref(false);
const ipVal = ref("");
function ipOk() {
  if (!ipVal.value) {
    message.error("请输入IP地址");
    return;
  }
  if (
    !/^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/.test(
      ipVal.value
    )
  ) {
    message.error("请输入正确的IP地址");
    return;
  }
  authCache.setServerIp(ipVal.value || null);
  message.success("设置成功");
  ipVisible.value = false;
}
function setIp() {
  ipVal.value = authCache.getServerIp() || "";
  ipVisible.value = true;
}
</script>
<style lang="less">
.login-bg {
  min-width: 720px;
  min-height: 720px;
}
.login {
  max-width: 432px;
  border: 1px solid #ebedf0;
  box-shadow: 0 45px 30px 0 rgba(41, 72, 152, 0.03), 0 23px 15px 0 rgba(41, 72, 152, 0.03),
    0 9px 7px 0 rgba(41, 72, 152, 0.02), 0 2px 3px 0 rgba(41, 72, 152, 0.01);
  border-radius: 8px;
  h2 {
    font-size: 24px;
    line-height: 36px;
    margin-bottom: 48px;
  }
  margin: 0 auto;
}
@media (max-width: 452px) {
  .login {
    margin: 0 10px;
  }
}
.page-right {
  min-width: 663px;
}
.sys-title {
  font-size: 40px;
  line-height: 50px;
  margin-bottom: 32px;
  color: #e08344;
}
.login-sys-title {
  font-size: 30px;
  color: #e08344;
  text-align: center;
}
.sys-des {
  font-size: 36px;
  line-height: 36px;
  color: #e08344;
}
@media (max-width: 1400px) {
  .sys-title {
    font-size: 36px;
    line-height: 46px;
    margin-bottom: 28px;
  }
  .sys-des {
    font-size: 34px;
    line-height: 32px;
  }
}
</style>
